package org.feifei.product.controller;



import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.util.CommonResult;
import org.feifei.basic.util.PageList;
import org.feifei.product.domain.Product;
import org.feifei.product.domain.ProductDetail;
import org.feifei.product.query.ProductQuery;
import org.feifei.product.service.ProductDetailService;
import org.feifei.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "服务接口", description = "服务接口详细描述")
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Resource
    private ProductDetailService productDetailService;



    //获取一个对象：get
    //@GetMapping相当于@RequestMapping(value="/xxx",method=RequestMethod.GET)
    //Get http://localhost:8080/product/1
    @ApiOperation(value = "查询单个服务", notes = "需要传递id")
    @GetMapping("/{id}")//  product/1    或 /product/2
    public Product findById(@PathVariable("id") Long id) {
        return productService.get(id);
    }


    //获取所有对象：get
    //Get http://localhost:8080/product
    @ApiOperation(value = "查询所有", notes = "不需参数")
    @GetMapping
    public List<Product> findAll() {
        return productService.list();
    }


    //分页 + 高级：post - 直接写，后面发送异步请求时再说
    //请求数据包中：请求行 + 请求头 + 请求体【参数就在请求体中】
    //Post http://localhost:8080/product + json参数productQuery
    @ApiOperation(value = "分页查询服务信息", notes = "需要传递当前页和每页显示的条数")
    @PostMapping("/list")
    public PageList findPage(@RequestBody ProductQuery productQuery) {
        return productService.listByPage(productQuery);
    }


    /**
     * 根绝传来的对象id，来删除对应的对象，同时需要线删除关联对象
     * @param id 对象id
     * @return
     */
    @ApiOperation("根据id删除服务")
    @DeleteMapping("/{id}")
    public CommonResult delete(@PathVariable("id")Long id) {
        try {
            productService.remove(id);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    /**
     * 根据对象的id，去获取关联对象的全部信息
     * @param id 关联对象的id
     * @return
     */
    @ApiOperation("根据服务信息获取服务详情")
    @GetMapping("/detail/{id}")
    public ProductDetail getProductDetailByProduct(@PathVariable("id")Long id) {
        return productDetailService.getByProductId(id);
    }

    /**
     * 添加或者修改的方法
     * @param product 需要管理的product对象，根据是否存在id来判断是删除还是修改
     * @return
     */
    @ApiOperation("修改或者添加服务")
    @PutMapping
    public CommonResult saveAndEdit(@RequestBody Product product) {
        try {
            productService.editORSave(product);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        }
    }

    /**
     * 根绝传来的对象id集合，来删除对应的对象集合，同时需要线删除关联对象集合
     * @param ids 对象id集合
     * @return
     */
    @PatchMapping
    @ApiOperation("批量删除")
    public CommonResult batchRemove(@RequestBody List<Long> ids) {
        try {
            productService.batchRemove(ids);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    /**
     * 批量上架方法
     * @param ids
     * @return
     */
    @ApiOperation("上架方法")
    @PostMapping("/onsale")
    public CommonResult onsale(@RequestBody List<Long> ids) {
        try {
            productService.onsale(ids);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    @ApiOperation("下架方法")
    @PostMapping("/offsale")
    public CommonResult offsale(@RequestBody List<Long> ids) {
        try {
            productService.offsale(ids);
            return CommonResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }

    @ApiOperation("领养服务的方法")
    @PostMapping("/adopt")
    public CommonResult adopt(@RequestBody Map<String, Long> map, HttpServletRequest request) {
        try {
            productService.adopt(map.get("id"), request);
            return CommonResult.ok();
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure();
        }
    }
}
